$(function(){
    //身份证
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var confirmationModal = $('#confirmationModal');
    var idCardChangeModal = $('#idCardChangeModal');
    var idCardModal = $('#idCardModal');
    $('#booking-container').on('click','.idcard-authentication',function(){
        var roomInfo = $(this).closest('.room-info');
        roomInfo.addClass('select').siblings().removeClass('select');
        var param = {};
        param.hotel = currentHotel;
        param.identity = roomInfo.find('.requestIdCardVal').val();
        ajaxRequest('/Home/Order/getOrderUserInfo', param, 'post', function(data) {
            var info = data.data.data;
            if(data.data.isBind){
                if(parseInt(info.idsex) == 1){
                    idCardModal.find('.idCard-sex').text(lang('male'));
                }else if(parseInt(info.idsex) == 0){
                    idCardModal.find('.idCard-sex').text(lang('female'));
                }else{
                    idCardModal.find('.idCard-sex').text('');
                }
                idCardModal.find('.idCard-name').text(info.name);
                idCardModal.find('.idCard-nation').text(info.idnation);
                idCardModal.find('.idCard-num').text(info.identity);
                idCardModal.find('.idCard-address').text(info.idaddress);
                idCardModal.modal('show');                
            }else{
                idCardChangeModal.find('.get-photo').removeClass('hidden');
                idCardChangeModal.find('.add-photo').addClass('hidden').html('');
                $("input[type='file']").val('');                
                idCardChangeModal.modal('show');
            }

        }, function(error) {
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        }); 
    });
    idCardModal.on('click','.header-reset',function(){
        idCardModal.modal('hide');
        idCardChangeModal.modal('show');
    });
    idCardChangeModal.on('click','.header-cancel', function (e) {
        e.preventDefault();
        e.stopPropagation();
        idCardChangeModal.modal('hide');
        $('.get-photo').removeClass('hidden');
        $('.add-photo').addClass('hidden').html(''); 
        $("input[type='file']").val('');
        window.face = '';
        window.back = '';
    });
    $('#submitEditIdCard').on('click',function(e){
        if(window.face == ''){
            toastr.error(lang('select.pic'));
            return false;
        }
        e.preventDefault();
        e.stopPropagation();
        toggleLoading(true); 
        Parmas = {};
        Parmas.baseface = window.face;
        if(window.back != ''){
            Parmas.baseback = window.back;
        }
        ajaxRequest('/Home/User/getIdInformation', Parmas, 'post',function(data){
            var info = data.data;
            toggleLoading(false);
            $('#submitConfirmation').data('info',info);
            confirmationModal.find('.idCard-name').text(info.name);
            confirmationModal.find('.idCard-sex').text(info.sex);
            confirmationModal.find('.idCard-nation').text(info.nationality);
            confirmationModal.find('.idCard-num').text(info.num);
            confirmationModal.find('.idCard-address').text(info.address);
            idCardChangeModal.modal('hide');
            confirmationModal.modal('show');
        }, function(error) {
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        }); 
    });    
    $('#submitConfirmation').on('click',function(){
        var info =$(this).data('info');
        var roomInfo = $('.room-info');
        for(var i=0;i<roomInfo.length;i++){
            if(roomInfo.eq(i).hasClass('select')){
                roomInfo.eq(i).find('.requestNameVal').val(info.name);
                roomInfo.eq(i).find('.requestIdCardVal').val(info.num);
                roomInfo.eq(i).data('nationality',info.nationality);
                roomInfo.eq(i).data('address',info.address);
                if(info.sex == lang('male')){
                    roomInfo.eq(i).data('sex',1);
                }else{
                    roomInfo.eq(i).data('sex',0);
                }
            }
        }
        confirmationModal.modal('hide');
    });
    confirmationModal.on('click','.confirmation-cancel', function (e) {
        e.preventDefault();
        e.stopPropagation();
        confirmationModal.modal('hide');
        $('.get-photo').removeClass('hidden');
        $('.add-photo').addClass('hidden').html(''); 
        $("input[type='file']").val('');
    }).on('click','.confirmation-back', function (e) {
        e.preventDefault();
        e.stopPropagation();
        confirmationModal.modal('hide');
        idCardChangeModal.modal('show');
    });
    $("input[type='file']").on('change', function (e) {  
        var self = $(this);  
        var file = e.target.files[0];
        drawOnCanvas(file,self);        
    }); 
    function drawOnCanvas(file,self) {
        var reader = new FileReader();
        var cameraContainer = self.closest('.camera-container');
        var value = cameraContainer.data('value');
        var width = cameraContainer.width();
        var height = cameraContainer.height();
        reader.onload = function(e) {
            var dataURL = e.target.result,
            canvas = document.querySelector('canvas'),
            ctx = canvas.getContext('2d'),
            img = new Image();
            img.onload = function() {
                var square = 320;
                canvas.width = square;
                canvas.height = square;
                var context = canvas.getContext('2d');
                context.clearRect(0, 0, square, square);
                var imageWidth;
                var imageHeight;
                var offsetX = 0;
                var offsetY = 0;
                if (this.width > this.height) {
                    imageWidth = Math.round(square * this.width / this.height);
                    imageHeight = square;
                    offsetX = -Math.round((imageWidth - square) / 2);
                } else {
                    imageHeight = Math.round(square * this.height / this.width);
                    imageWidth = square;
                    offsetY = -Math.round((imageHeight - square) / 2);
                }
                context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);
                var base64 = canvas.toDataURL('image/jpeg', 0.7);
                if(parseInt(value) == 1){
                   window.face = base64; 
                }else{
                    window.back = base64;
                }
                cameraContainer.find('.get-photo').addClass('hidden');
                cameraContainer.find('.add-photo').removeClass('hidden').append(img);
                cameraContainer.find('img').css({'width':width,'height':height});
            };
            img.src = dataURL;
        };
        reader.readAsDataURL(file);
    }       
});
